import json
import requests
import pymysql

db_online = pymysql.connect(host="121.4.69.146", user='root', password='xhhg@2020', port=3306, db="big_data",
                            use_unicode=True, charset='utf8')
cursor_online = db_online.cursor()


def get_login():
    headers = {"Accept": "application/json", }
    # url = 'http://test.api.mingyukehui.com/api/auth/login'
    url = 'https://api.xinhehonggu.com/api/auth/login'
    phone = '18866668888'
    password = '888888'
    source = 'admin'
    data = {
        'method': '1',
        'phone': phone,
        'password': password,
        'source': source,
    }
    r = requests.post(url=url, data=data, headers=headers, verify=False)
    res = r.text
    res_dict = json.loads(res)
    token = res_dict['data']['token']
    return token


def get_content0(db_online, cursor_online):
    # sql = "select application_number, MIN(id) id from money WHERE id > 165 GROUP BY application_number ORDER BY id ASC"
    sql = "select application_number, MIN(id) id from money_copy1 GROUP BY application_number ORDER BY id ASC"
    cursor_online.execute(sql)
    content = cursor_online.fetchall()
    return content


def get_annel(application_number):
    sql = f"select application_number,money_type,money,close_date,pay_status from money_copy1 where application_number='{application_number}'"
    cursor_online.execute(sql)
    content = cursor_online.fetchall()
    item = {}
    item['application_number'] = application_number
    for i in range(len(content)):
        if '2021' in content[i][3]:
            if '年费' in content[i][1]:
                item['current_money'] = content[i][2]
                item['current_close_date'] = content[i][3]
                item['current_status'] = content[i][4]
                if item['current_status'] == '未缴费':
                    item['current_status'] = 2
    for i in range(len(content)):
        if '2022' in content[i][3]:
            if '年费' in content[i][1]:
                item['next_money'] = content[i][2]
                item['next_close_date'] = content[i][3]
    for i in range(len(content)):
        if '印花税' in content[i][1]:
            if content[i][3].split("-")[0] == '2021':
                item['current_money'] = str(int(item['current_money']) + 5)
            if content[i][3].split("-")[0] == '2022':
                item['next_money'] = str(int(item['next_money']) + 5)
    return item


def upload_annel(item):
    token = get_login()
    headers = {"Accept": "application/json", "token": token}
    url = 'http://test.api.mingyukehui.com/api/finalsAnnualFee'
    response = requests.post(url=url, data=item, headers=headers, verify=True).json()
    print(response)


def insert_info():
    content = get_content0(db_online, cursor_online)  # 拿到所有sqh
    for i in range(0, len(content)):
        print(f"当前正在插入第{i + 1}条数据，共{len(content)}条！")
        application_number = content[i][0]
        print(application_number)
        item = get_annel(application_number)
        # print(item)
        if item:
            if 'current_money' in item.keys() or 'next_money' in item.keys():
                print(item)
                upload_annel(item)


insert_info()
